<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表
class Detail_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->bbs_article = 'bbs_article';
        $this->bbs_article_category = 'bbs_article_category';
        $this->bbs_account = 'bbs_account';
        $this->users_info = 'users_info';
        $this->bbs_count = 'bbs_count';
        $this->bbs_reward = 'bbs_reward';
        $this->bbs_article_comment = 'bbs_article_comment';
        $this->bbs_like = 'bbs_like';
        $this->bbs_favorite = 'bbs_favorite';
        $this->bbs_letter = 'bbs_letter';
        $this->bbs_focus = 'bbs_focus';
        $this->bbs_fans = 'bbs_fans';
        $this->bbs_report = 'bbs_report';
    }
    function info($where){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name,'.$this->bbs_article_category.'.pid,'.$this->users_info.'.nickname,'.$this->users_info.'.user_id,'.$this->users_info.'.head_img,'.$this->bbs_account.'.balance,'.$this->bbs_account.'.level,'.$this->bbs_count.'.articles_count');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->join($this->bbs_account,$this->bbs_account.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->join($this->bbs_count,$this->bbs_count.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->where($where);
        return $this->db->get()->result_array();
    }
    function hot(){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->where('hot',1);
        $this->db->where('status',1);
        $this->db->order_by($this->bbs_article.'.create_time','desc');
        $this->db->limit(3,0);
        return $this->db->get()->result_array();
    }
    function recommend(){
        $this->db->select('id,name');
        $this->db->from($this->bbs_article_category);
        $this->db->where('is_show',0);
        $this->db->where('recommend',1);
        $this->db->order_by('sort','asc');
        $this->db->limit(3,0);
        return $this->db->get()->result_array();
        
    }
    function reward($article_id){
        $this->db->select($this->users_info.'.head_img');
        $this->db->from($this->bbs_reward);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_reward.'.user_id',left);
        $this->db->where($this->bbs_reward.'.article_id',$article_id);
        $this->db->order_by($this->bbs_reward.'.create_time','desc');
        $this->db->limit(8,0);
        return $this->db->get()->result_array();
    }
    function comcount($where){
        $this->db->select('*');
        $this->db->from($this->bbs_article_comment);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    function comment($where,$page,$limit){
        $this->db->select($this->bbs_article_comment.'.*,'.$this->users_info.'.nickname,'.$this->users_info.'.head_img,'.$this->users_info.'.signature,'.$this->bbs_account.'.level,'.$this->bbs_account.'.balance,'.$this->bbs_count.'.articles_count,'.$this->bbs_count.'.fans_count');
        $this->db->from($this->bbs_article_comment);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->join($this->bbs_account,$this->bbs_account.'.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->join($this->bbs_count,$this->bbs_count.'.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.'.create_time','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
        
    }
    function addcomment($data){
        $this->db->insert($this->bbs_article_comment,$data);
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('commentnum','commentnum+1',FALSE);
           $this->db->where('id',$data['article_id']);
           $this->db->update($this->bbs_article);
           //count表添加记录
           $this->db->set('reply_count','reply_count+1',FALSE);
           $this->db->where('id',$data['uid']);
           $this->db->update($this->bbs_count);
        }
        return $this->db->affected_rows();
    }
    function category($pid){
        $this->db->select('id,name');
        $this->db->from($this->bbs_article_category);
        $this->db->where('pid',$pid);
        $this->db->where('is_show','0');
        $this->db->order_by('sort','asc');
        return $this->db->get()->result_array();
    }
    function reads($article_id){        //阅读数统计
        $this->db->set('readnum','readnum + 1',FALSE);
        $this->db->where('id',$article_id);
        $this->db->update($this->bbs_article);
    }
    function likes($article_id){
        $this->db->select('likes');
        $this->db->from($this->bbs_article);
        $this->db->where('id',$article_id);
        return $this->db->get()->row_array(); 
    }
    function likestatus($checkinfo){
        $this->db->select('*');
        $this->db->from($this->bbs_like);
        $this->db->where($checkinfo);
        return $this->db->get()->num_rows();
    }
    function favstatus($checkinfo){
        $this->db->select('*');
        $this->db->from($this->bbs_favorite);
        $this->db->where($checkinfo);
        return $this->db->get()->num_rows();
    }
    function addlike($data){
        $this->db->insert($this->bbs_like,$data);
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('likes','likes+1',FALSE);
           $this->db->where('id',$data['article_id']);
           $this->db->update($this->bbs_article);
        }
        return $this->db->affected_rows();
    }
    function addfav($data){
        $this->db->insert($this->bbs_favorite,$data);
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('favor_count','favor_count+1',FALSE);
           $this->db->where('user_id',$data['user_id']);
           $this->db->update($this->bbs_count);
        }
        return $this->db->affected_rows();
    }
    function addpost($data){
        $this->db->insert($this->bbs_article,$data);
        if($this->db->affected_rows()==1){
           $this->db->set('articles_count','articles_count+1',FALSE);
           $this->db->where('user_id',$data['authorid']);
           $this->db->update($this->bbs_count);
        }
        return $this->db->affected_rows();
    }
    function send($data){
        $this->db->insert($this->bbs_letter,$data);
        return $this->db->affected_rows();
    }
    function checkfocus($where){
        $this->db->select("*");
        $this->db->from($this->bbs_focus);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    function focus($data){
        $this->db->insert($this->bbs_focus,$data);
        return $this->db->affected_rows();
    }
    function fanstatus($where2){
        $this->db->select("*");
        $this->db->from($this->bbs_fans);
        $this->db->join($this->bbs_article,$this->bbs_article.'.authorid='.$this->bbs_fans.'.user_id',left);
        $this->db->where($where2);
        return $this->db->get()->num_rows();
    }
    function checkfan($where){
        $this->db->select("*");
        $this->db->from($this->bbs_fans);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    function addfan($data){
        $this->db->insert($this->bbs_fans,$data);
        if($this->db->affected_rows()==1){
           $this->db->set('fans_count','fans_count+1',FALSE);
           $this->db->where('user_id',$data['user_id']);
           $this->db->update($this->bbs_count); 
           $this->db->set('following_count','following_count+1',FALSE);
           $this->db->where('user_id',$data['followuid']);
           $this->db->update($this->bbs_count); 
        }
        return $this->db->affected_rows();
    }
    function catinfo($id){
        $this->db->select("name,group");
        $this->db->from($this->bbs_article_category);
        $this->db->where('id',$id);
        return $this->db->get()->row_array();
    }
    function report($data){
        $this->db->insert($this->bbs_report,$data);
        return $this->db->affected_rows();
    }
}
